package com.hansen.library.pickerimage.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.hansen.library.Constants;
import com.hansen.library.R;
import com.hansen.library.ui.fragment.BaseFragment;
import com.hansen.library.pickerimage.PickImageApplication;
import com.hansen.library.pickerimage.widget.photoview.OnOutsidePhotoTapListener;
import com.hansen.library.pickerimage.widget.photoview.OnPhotoTapListener;
import com.hansen.library.pickerimage.widget.photoview.PhotoView;

/**
 * Created by han on 2019/6/15 0015.
 */

public class PicturePreviewFragment extends BaseFragment implements OnPhotoTapListener, OnOutsidePhotoTapListener {

    private final int LOAD_FAIL = 0x01;
    private final int LOAD_SUCESS = 0x02;

    //photview
    private PhotoView pv_pic;
    //圆形进度条
    private ProgressBar pbar_preview;

    private Handler mMainThreadHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case LOAD_SUCESS:
                    pbar_preview.setVisibility(View.GONE);
                    break;
                case LOAD_FAIL:
                    pbar_preview.setVisibility(View.GONE);
                    Toast.makeText(PickImageApplication.getInstance().getContext(), getString(R.string.tips_img_load_error), Toast.LENGTH_SHORT);
                    break;
                default:
                    break;
            }
        }
    };

    public static PicturePreviewFragment newInstance(String imageUrl) {
        PicturePreviewFragment mFragment = new PicturePreviewFragment();

        Bundle bundle = new Bundle();

        bundle.putString(Constants.KeyUrl, imageUrl);

        mFragment.setArguments(bundle);

        return mFragment;
    }

    @Override
    protected int getResID() {
        return R.layout.fragment_picture_preview;
    }

    @Override
    protected void initView(View mView) {
        pv_pic = mView.findViewById(R.id.pv_pic);

        pbar_preview = mView.findViewById(R.id.pbar_preview);
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        loadPicture();
    }

    @Override
    protected void listener() {
        pv_pic.setOnPhotoTapListener(this);
        pv_pic.setOnOutsidePhotoTapListener(this);
    }

    @Override
    protected void widgetClick(View v) {

    }

    //加载图片
    private void loadPicture() {
        if (getArguments() != null) {

            String imgUrl = getArguments().getString(Constants.KeyUrl);

            Glide.with(mContext)
                    .load(imgUrl)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            mMainThreadHandler.sendEmptyMessage(LOAD_FAIL);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, com.bumptech.glide.load.DataSource dataSource, boolean isFirstResource) {
                            mMainThreadHandler.sendEmptyMessage(LOAD_SUCESS);
                            return false;
                        }
                    }).into(pv_pic);
        }
    }

    @Override
    public void onPhotoTap(ImageView view, float x, float y) {
        if (getActivity() != null) {
            getActivity().finish();
            getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        }
    }

    @Override
    public void onOutsidePhotoTap(ImageView imageView) {
        if (getActivity() != null) {
            getActivity().finish();
            getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        }
    }

    @Override
    public void onDestroy() {
        if (mMainThreadHandler != null) {
            mMainThreadHandler.removeCallbacksAndMessages(null);
        }
        mMainThreadHandler = null;
        super.onDestroy();
    }
}
